<template>
  <el-dialog
    title="编辑"
    v-model="createPopShow"
    width="30%"
    center
    :show-close="false"
  >
    <el-form :model="createData" ref="ruleFromRef">
      <el-form-item
        label="课程标题"
        prop="title"
        required
      >
        <el-input v-model="createData.title"></el-input>
      </el-form-item>

      <el-form-item label="封面链接" prop="course_img" required>
        <el-input v-model="createData.course_img"></el-input>
      </el-form-item>

      <el-form-item label="课程价格" prop="price" required>
        <el-input v-model="createData.price"></el-input>
      </el-form-item>

      <el-form-item label="课程评分" prop="point" required>
        <el-input v-model="createData.point"></el-input>
      </el-form-item>

      <el-form-item label="课程类别" prop="category" required>
        <el-input v-model="createData.category"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="confirmCreateClick('cancel')">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFromRef)"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { defineProps, reactive, ref } from 'vue';

const { createPopShow, confirmCreateClick } = defineProps([
  'createPopShow',
  'confirmCreateClick',
]);

/**
 * 新增组件的维护
 */

// const createData = reactive({
//   title: '',
//   course_img: '',
//   price: '',
//   point: '',
//   category: '',
// });

const createData = reactive({
  title: 'HTML5权威指南',
  course_img:
    'https://pic2.zhimg.com/80/v2-2fe5ca4fe8593066ee82f486ef11532d_720w.webp',
  price: '25.3',
  point: '8.5',
  category: 'front',
});
/**
 * 表单校验
 */

const ruleFromRef = ref(null);

const submitForm = async (formEl) => {
  if (!formEl) {
    // 未获得表单对象
    return;
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      //TODO: 调用父组件传过来的提交数据的方法
      confirmCreateClick({
        title: createData.title,
        course_img: createData.course_img,
        price: createData.price,
        point: createData.point,
        category: createData.category,
      });

    } else {
      ElMessage({
        message: '数据填写不完整',
        type: 'warning',
        showClose: true,
      });
    }
  });
};


</script>
<style lang="less" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

:deep(.el-form-item__label) {
  width: 80px !important;
}

:deep(.el-form-item__content) {
  justify-content: center;
  margin-left: 0 !important;
}
</style>
